<?php

function al_setcookie($username, $password, $already_md5 = true) {
    if (!$already_md5) {
        $password = md5(md5($password)); // Double hash the password in the cookie.
    }
    setcookie(USER_COOKIE, $username, 0, COOKIEPATH, COOKIE_DOMAIN);
    setcookie(PASS_COOKIE, $password, 0, COOKIEPATH, COOKIE_DOMAIN);
}

function al_getcookie() {
    $usercookie['username'] = null;
    $usercookie['password'] = null;
    if (!empty($_COOKIE[USER_COOKIE]) && !empty($_COOKIE[PASS_COOKIE])) {
        $usercookie['username'] = $_COOKIE[USER_COOKIE];
        $usercookie['password'] = $_COOKIE[PASS_COOKIE];
    }
    return $usercookie;
}

function al_clearcookie() {
    setcookie(USER_COOKIE, ' ', time() - 31536000, COOKIEPATH, COOKIE_DOMAIN);
    setcookie(PASS_COOKIE, ' ', time() - 31536000, COOKIEPATH, COOKIE_DOMAIN);
    setcookie(USER_COOKIE, ' ', time() - 31536000, SITECOOKIEPATH, COOKIE_DOMAIN);
    setcookie(PASS_COOKIE, ' ', time() - 31536000, SITECOOKIEPATH, COOKIE_DOMAIN);
}

function get_user_group_id($username) {
    global $aldb;
    return $aldb->get_var("SELECT user_group FROM $aldb->users WHERE username='$username'");
}

function get_username_by_id($id) {
    global $aldb;
    if (empty($id))
        return null;
    return $aldb->get_var("SELECT username FROM $aldb->users WHERE id='$id'");
}

function current_user_draft_count() {
    global $aldb;
    $user_id = ArlicleUser::user_id();
    return $aldb->get_var("SELECT count(id) FROM $aldb->posts WHERE poster_id='$user_id' AND post_status='2'");
}

function get_group_title($group_id) {
    global $aldb;
    return $aldb->get_var("SELECT title FROM $aldb->groups WHERE id='$group_id'");
}

function get_capability($id, $key, $type = 'group') {
    global $aldb;
    if ($type == 'group') {
        $condition = "group_id='$id'";
    } else {
        $condition = "user_id='$id'";
    }
    $value = $aldb->get_var("SELECT `value` FROM $aldb->capabilities WHERE `key`='$key' AND $condition LIMIT 1");
    if (!empty($value)) {
        $value = unserialize($value);
    }
    return $value;
}
if ( !function_exists('time_format') ) {
    function time_format($time) {
        return date(get_option('time_format'),$time);
    }
}

function get_category_name($id) {
    global $aldb;
    return $aldb->get_var("SELECT `title` FROM $aldb->diagram WHERE id='$id'");
}

function save_session($data = null) {
    global $aldb;

    $session_created = env('REQUEST_TIME');
    $session_user_id = ArlicleUser::user_id();
#    $aldb->query("DELETE FROM $aldb->sessions WHERE user_id='$session_user_id'");

    $session_id = md5(ARLICLE . $session_created . rand());
    $aldb->query("INSERT INTO $aldb->sessions (`id`, `data`, `user_id`, `created`) VALUES ('$session_id', '$data', '$session_user_id', '$session_created' )");
    return $session_id;
}

function has_group() {
    global $aldb;
    return $aldb->get_var("SELECT `id` FROM $aldb->groups LIMIT 1");
}

function has_category() {
    global $aldb;
    return $aldb->get_var("SELECT `id` FROM $aldb->diagram WHERE `type`='list' LIMIT 1");
}

# cache the diagram urls
function al_cache_urls() {
    global $aldb;
    $diagrams = $aldb->get_results("SELECT url, metavalue FROM $aldb->diagram ORDER BY displayorder ASC");
    $urls = array();
    foreach($diagrams as $diagram) {
        $metavalue = unserialize($diagram->metavalue);
        if ($metavalue['template'] !== '') {
            $urls[$diagram->url] = $metavalue['template'];
        }
    }
    $content = serialize($urls);
    $handle = fopen(TMP_PATH . 'themes/urls.php', 'w+');
    fwrite($handle, $content);
    fclose($handle);
}

function activate_plugin($plugin) {
    global $aldb;
    $current = get_option('active_plugins');
    $plugin = trim($plugin);

    if ( !in_array($plugin, $current) ) {
        $current[] = $plugin;
        sort($current);
        update_option('active_plugins', $current);
    }
    require_once ROOT . 'www/plugins/' . $plugin . '/ini.php';
    if (file_exists(ROOT . 'www/plugins/' . $plugin . '/libs/install.php')) {
        if (!file_exists(ROOT . 'www/tmp/plugins/' . $plugin . '_installed')) {
            require ROOT . 'www/plugins/' . $plugin . '/libs/install.php';
            $handle = fopen(ROOT . 'www/tmp/plugins/' . $plugin . '_installed', 'w');
            fwrite($handle, '');
            fclose($handle);
        }
    }
    return null;
}

function deactivate_plugin($plugin) {
    $current = get_option('active_plugins');
    foreach ($current as $key => $value) {
        if ($plugin == $value) {
            unset($current[$key]);
        }
    }
    sort($current);
    update_option('active_plugins', $current);
}

function get_diagram_metavalue($key, $metavalue) {
    $values = unserialize($metavalue);
    if (isset($values[$key])) {
        return $values[$key];
    }
}

function get_post_url($category_url, $post) {
    if (!empty($post->post_slug)) {
        $post_url = $post->post_slug;
    } else {
        $post_url = $post->id;
    }
    return "{$category_url}post/$post_url/";
}

function get_all_pages() {
    global $aldb;
    return $aldb->get_results("SELECT * FROM $aldb->diagram WHERE `type`='page'");
}

function get_all_posts() {
    global $aldb;
    return $aldb->get_results("SELECT p.id, p.post_title, p.post_content, p.post_time, p.post_slug, u.username, d.title as category_title, d.url as category_url FROM $aldb->posts as p LEFT JOIN $aldb->post2category as c ON p.id=c.post_id LEFT JOIN $aldb->diagram as d ON c.category_id=d.id LEFT JOIN $aldb->users as u ON p.poster_id=u.id WHERE p.post_status='1' ORDER BY p.post_time DESC LIMIT 10");
}
?>
